function dateNumberConvert(val) {
  return val < 10 ? "0" + val : "" + val;
}

module.exports.getQueryString = function(name) {
  var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
  var r = decodeURI(window.location.search.substr(1)).match(reg);
  if (r != null) return unescape(r[2]);
  return null;
};

module.exports.formatTime = function(value, type) {
  let dataTime = "";
  value = value.replace(new RegExp("/", "gm"), "-");
  let data = new Date(value);
  let year = data.getFullYear();
  let month = data.getMonth() + 1;
  let day = data.getDate();
  let hour = data.getHours();
  let minute = data.getMinutes();
  let second = data.getSeconds();
  month < 10 ? (month = "0" + month) : month;
  day < 10 ? (month = "0" + day) : day;
  hour < 10 ? (hour = "0" + hour) : hour;
  minute < 10 ? (minute = "0" + minute) : minute;
  second < 10 ? (second = "0" + second) : second;
  if (type == "YMD") {
    dataTime = year + "年" + month + "月" + day + "日";
  } else if (type == "YMDHMS") {
    dataTime =
      year +
      "-" +
      month +
      "-" +
      day +
      "  " +
      hour +
      ":" +
      minute +
      ":" +
      second;
  } else if (type == "HMS") {
    dataTime = hour + ":" + minute + ":" + second;
  } else if (type == "YM") {
    dataTime = year + "年" + month + "月";
  }
  return dataTime; // 将格式化后的字符串输出到前端显示
};

module.exports.themeCovertType = function(value) {
  switch (value) {
    case 1:
      return 10;
    case 5:
      return 10;
    case 6:
      return 10;
    case 4:
      return 10;
    case 7:
      return 10;
    case 122:
      return 11;
    case 112:
      return 12;
    case 118:
      return 13;
    default:
      return "";
  }
};

module.exports.uuid = function() {
  var s = [];
  var hexDigits = "0123456789abcdef";
  for (var i = 0; i < 36; i++) {
    s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
  }
  s[14] = "4"; // bits 12-15 of the time_hi_and_version field to 0010
  s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1); // bits 6-7 of the clock_seq_hi_and_reserved to 01
  s[8] = s[13] = s[18] = s[23];

  var uuid = s.join("");
  return uuid;
};

module.exports.uuidTime = function() {
  var nowDate = new Date();
  var timestamp =
    new Date(
      nowDate.getFullYear() +
        "-" +
        (nowDate.getMonth() + 1) +
        "-" +
        nowDate.getDate()
    ).getTime() / 1000;
  return timestamp;
};

// 秒数转分钟
module.exports.getDurationTime = function(during) {
  var s = Math.floor(during / 1) % 60;
  during = Math.floor(during / 60);
  var i = during % 60;
  during = Math.floor(during / 60);
  var h = during % 24;
  during = Math.floor(during / 24);
  var d = during;
  return { d, h, i, s };
};

/*
dateStr 日期字符串
connect 连接符号
*/
module.exports.formatDate = function(dateStr, connect) {
  if (!dateStr) return;
  const date = new Date(dateStr);
  return (
    date.getFullYear() +
    connect +
    dateNumberConvert(date.getMonth() + 1) +
    connect +
    dateNumberConvert(date.getDate())
  );
};
